********************************************************************************
** 	TITLE:		ON03_results                                                  ** 	
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       ON03_results                                                  **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* 1. Download the "Valid Votes Cast for Each Candidate" file from this link: https://results.elections.on.ca/api/report-groups/5/report-outputs/521/csv
* 2. Save the file to the CSV format with the following name: "ON03_results.csv"

* Import the dataset

import delimited "ON03_results.csv"

* Keep only general election

drop if isgeneralelection == 0

* Rename and (re)create variables

rename electoraldistrictnumber districtnumber03
rename electoraldistrictnameenglish districtname03
rename politicalinterestcode partyname03
rename totalvalidballotscast numberofvotes03
gen numberofvotespercent03 = percentoftotalvalidballotscast*100

* Generate dichotomous variable for each party

gen lib03 = 1 if partyname03 == "L"
replace lib03 = 0 if lib03!=1

gen con03 = 1 if partyname03 == "PC"
replace con03 = 0 if con03!=1

gen ndp03 = 1 if partyname03 == "ND"
replace ndp03 = 0 if ndp03!=1

gen green03 = 1 if partyname03 == "GP"
replace green03 = 0 if green03!=1

* Generate party variable

gen party03 = 1 if lib03 == 1
replace party03 = 2 if con03 == 1
replace party03 = 3 if ndp03 == 1
replace party03 = 4 if green03 == 1
replace party03 = 88 if party03 == . 

* Generate rank of parties

gsort districtname03 -numberofvotes03
bysort districtname03 : gen rank03 = _n

gen first03 = party03 if rank03 == 1
gen second03 = party03 if rank03 == 2
gen third03 = party03 if rank03 == 3

bysort districtname03 : gen winner03 = first03[1]
bysort districtname03 : gen runnerup03 = second03[2]
bysort districtname03 : gen thirdplace03 = third03[3]

* Generate margins of victory

bysort districtname03 : gen margin03 = numberofvotespercent03[1] - numberofvotespercent03[2]

* Generate effective number of electoral parties

gen sqrvotepr03 = (numberofvotespercent03/100)^2

bysort districtname03 : egen sumsqrvotepr03 = sum(sqrvotepr03)

gen enep03 = 1/sumsqrvotepr03

* Labelling parties

label define party 1 "Liberal Party" 2 "Progressive Conservative Party" 3 "New Democratic Party" 4 "Green Party" 88 "Other"
label values winner03 runnerup03 thirdplace03 party

* Selecting, ordering, and sorting variables

keep districtnumber03 districtname03 party03 rank03 numberofvotes03 numberofvotespercent03 margin03 enep03 winner03 runnerup03 thirdplace03

order districtnumber03 districtname03 party03 rank03 numberofvotes03 numberofvotespercent03 margin03 enep03 winner03 runnerup03 thirdplace03

sort districtnumber03 rank03

drop if rank03 > 3

* Drop duplicates

duplicates tag districtnumber03, gen(dup)
duplicates drop districtnumber03, force
drop dup

* Create election variable

gen election03 = "2003 Ontario general election"

* Saving file

save ON03_results, replace

* Merge with ON03_results.dta and generate reelection variable

merge 1:1 _n using ON99_results.dta

* Generate variable for incumbent party reelection

gen reelected03 = 1 if winner03 == winner99
replace reelected03 = 0 if reelected03!=1

* Saving file

save ON9903_results, replace